ASP.NET Core 部署到 IIS 进行托管
(给DotNet加星标,提升.Net技能)
转自:Ron.Liang
cnblogs.com/viter/p/10388902.html
前言
1.2 修改应用程序池为:无托管代码/集成
1.3 映射演示域名
修改 C:\Windows\System32\drivers\etc\hosts 文件映射如果下
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
172.16.10.227 www.di.com # 这里的域名就是在 IIS 中设置的演示域名
1.5 访问部署好的网站
在浏览器中输入地址:http://www.di.com/
好了,现在已经部署成功了。虽然简单粗暴,但是我们干出来了,为了避免以后会使用到各种部署姿势,下面一个小节的内容你还需要了解一下。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore"
path="*" verb="*"
modules="AspNetCoreModuleV2"
resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet"
arguments=".\Deploy.IIS.dll"
stdoutLogEnabled="false"
stdoutLogFile=".\logs\stdout"
hostingModel="outofprocess" />
</system.webServer>
</location>
</configuration>
<!--ProjectGuid: ea8ea1cd-a655-48c6-ad48-1cca646c2db7-->
进程外托管
选择进程外托管时,web.config 配置节点 system.webServer/aspNetCore.hostingModel 的值必须设置为:outofprocess,选择进程外托管,实际上就是告诉 IIS ,当前应用程序不使用 IISHttpServer,改为使用 Kestrel 服务器
不同托管模式下代码的变化
当你在 Program.cs 中使用默认的代码创建服务器的时候,不管使用的是 inprocess 还是 outofprocess ,代码是无需改变的,就像下面的代码,其中,要关注的代码是:WebHost.CreateDefaultBuilder(args),表示使用默认的构建
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
强烈建议使用 WebHost.CreateDefaultBuilder(args) 的默认构造,别去踩那么多的坑
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
return new WebHostBuilder()
.UseKestrel()
.UseIIS()
.UseIISIntegration()
.UseStartup<Startup>();
}
}
结束语
好了,今天就到这里,为了更灵活的针对各种部署环境进行发布预热,大家可以通过 dotnet publish -? 来学习更多发布命令的配置
代码下载
https://github.com/lianggx/EasyAspNetCoreDemo/tree/master/Deploy.IIS
推荐阅读
(点击标题可跳转阅读)
看完本文有收获?请转发分享给更多人
关注「DotNet」加星标,提升.Net技能
喜欢就点一下「好看」呗~